package cn.edu.nuc.ssm.controller;

import cn.edu.nuc.ssm.entity.BbsTopic;
import cn.edu.nuc.ssm.service.BbsTopicService;
import cn.edu.nuc.ssm.util.IPTimeStamp;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2017/5/13.
 */
@Controller
@RequestMapping("/bbsindex")
public class BackBbsIndexController {

    @Autowired
    private BbsTopicService bbsTopicService;

    @RequestMapping(value = "/tolist",method = RequestMethod.GET)
    public String tolist() {

        return "back/bbsindex/list";
    }

    @RequestMapping(value = "/tohotlist",method = RequestMethod.GET)
    public String tohotlist() {

        return "back/bbsindex/tohotlist";
    }

    @RequestMapping(value = "/tolistbo",method = RequestMethod.GET)
    public String tolistbo() {

        return "back/bbsindex/listbo";
    }

    @RequestMapping(value = "/tolisthit",method = RequestMethod.GET)
    public String tolisthit() {

        return "back/bbsindex/tohitlist";
    }

    @RequestMapping(value = "/golisthit",method = RequestMethod.GET)
    public String managerlisthit() {

        return "back/bbsindex/listhit";
    }

    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<BbsTopic> list() {

        List<BbsTopic> bbsTopicList = bbsTopicService.findNolocation();

        return bbsTopicList;
    }

    @ResponseBody
    @RequestMapping(value = "/listbo",method = RequestMethod.GET)
    public List<BbsTopic> listbo() {

        List<BbsTopic> bbsTopicList = bbsTopicService.findbolocation();

        return bbsTopicList;
    }

    @ResponseBody
    @RequestMapping(value = "/listhot",method = RequestMethod.GET)
    public List<BbsTopic> listhot() {

        List<BbsTopic> bbsTopicList = bbsTopicService.findhotlocation();

        return bbsTopicList;
    }

    @ResponseBody
    @RequestMapping(value = "/listhit",method = RequestMethod.GET)
    public List<BbsTopic> listhit() {

        List<BbsTopic> bbsTopicList = bbsTopicService.findhotlocation();

        return bbsTopicList;
    }

    @ResponseBody
    @RequestMapping(value = "/jsonlisthit",method = RequestMethod.GET)
    public List<BbsTopic> golisthit() {

        List<BbsTopic> bbsTopicList = bbsTopicService.findhitlocation();

        return bbsTopicList;
    }

    @RequestMapping(value = "/tolisthot",method = RequestMethod.GET)
    public String golisthotpage() {

        return "back/bbsindex/listhot";
    }



    @ResponseBody  //  addhot
    @RequestMapping(value = "/addbo",method = RequestMethod.GET)
    public String delete(Integer id){

        bbsTopicService.addbo(id);

        return "success";
    }

    @ResponseBody  //  addhot
    @RequestMapping(value = "/addhot",method = RequestMethod.GET)
    public String addhot(Integer id){

        bbsTopicService.addhot(id);

        return "success";
    }

    @ResponseBody
    @RequestMapping(value = "/addhit",method = RequestMethod.GET)
    public String addhit(Integer id){

        bbsTopicService.addhit(id);

        return "success";
    }


    @RequestMapping(value = "/toupdatebopic",method = RequestMethod.GET)
    public String toupdatebopic(Integer id){

        return "back/bbsindex/updatebopic";
    }

    @RequestMapping(value = "/toupdatehotpic",method = RequestMethod.GET)
    public String toupdatehotpic(Integer id){

        return "back/bbsindex/updatehotpic";
    }

    @RequestMapping(value = "/toupdatehitpic",method = RequestMethod.GET)
    public String toupdatehitpic(Integer id){

        return "back/bbsindex/updatehitpic";
    }

    @ResponseBody
    @RequestMapping(value = "/removebo",method = RequestMethod.GET)
    public String removebo(Integer id){

        bbsTopicService.removebo(id);

        return "success";
    }

    //添加
    @RequestMapping(value = "/upload",method=RequestMethod.POST)
    public String upload( @RequestParam("id") Integer id, @RequestParam("file")MultipartFile file, HttpServletRequest request) throws IOException {


        if (id==null){

            throw new RuntimeException();
        }

        String realPath = request.getSession().getServletContext()
                .getRealPath("/upload/bbs");

        File pathFile = new File(realPath);
        if (!pathFile.exists()) {

            pathFile.mkdir();
        }
        IPTimeStamp ipTimeStamp = new IPTimeStamp();
        //获得新文件的名字
        String timeStamp = ipTimeStamp.getTimeStamp();

        //获得上传文件的后缀
        String ext = FilenameUtils.getExtension(file.getOriginalFilename());
        //并新文件的名称
        String newFilename = timeStamp + "." + ext;

        FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath, newFilename));


        bbsTopicService.updatepicbyid(id,newFilename);


        return "redirect:/bbsindex/tolistbo";


    }

    //添加
    @RequestMapping(value = "/uploadhot",method=RequestMethod.POST)
    public String uploadhot( @RequestParam("id") Integer id, @RequestParam("file")MultipartFile file, HttpServletRequest request) throws IOException {

        if (id==null){

            throw new RuntimeException();
        }
        String realPath = request.getSession().getServletContext()
                .getRealPath("/upload/bbs");
        File pathFile = new File(realPath);
        if (!pathFile.exists()) {

            pathFile.mkdir();
        }
        IPTimeStamp ipTimeStamp = new IPTimeStamp();
        //获得新文件的名字
        String timeStamp = ipTimeStamp.getTimeStamp();

        //获得上传文件的后缀
        String ext = FilenameUtils.getExtension(file.getOriginalFilename());
        //并新文件的名称
        String newFilename = timeStamp + "." + ext;

        FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath, newFilename));

        bbsTopicService.updatepicbyid(id,newFilename);
        return "redirect:/bbsindex/tolisthot";
    }

    //添加
    @RequestMapping(value = "/uploadhit",method=RequestMethod.POST)
    public String uploadhit( @RequestParam("id") Integer id, @RequestParam("file")MultipartFile file, HttpServletRequest request) throws IOException {

        if (id==null){

            throw new RuntimeException();
        }
        String realPath = request.getSession().getServletContext()
                .getRealPath("/upload/bbs");
        File pathFile = new File(realPath);
        if (!pathFile.exists()) {

            pathFile.mkdir();
        }
        IPTimeStamp ipTimeStamp = new IPTimeStamp();
        //获得新文件的名字
        String timeStamp = ipTimeStamp.getTimeStamp();

        //获得上传文件的后缀
        String ext = FilenameUtils.getExtension(file.getOriginalFilename());
        //并新文件的名称
        String newFilename = timeStamp + "." + ext;

        FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath, newFilename));

        bbsTopicService.updatepicbyid(id,newFilename);
        return "redirect:/bbsindex/golisthit";
    }


}
